package com.jic.market.impl.rest.discount.model;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionMemberGrade;
import com.jic.market.enums.DiscountDetailStatusEnum;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.response.rest.coupon.QueryCouponPersonResponse;
import com.jic.market.response.rest.discount.SkillTime;
import com.jic.market.response.rest.groupon.PostPayGroupInstDetailInfo;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yinhaijun
 * @date: 2020/4/27
 */
@Data
public class GoodsDetailModel implements Serializable,BaseDiscountModel {

    private static final long serialVersionUID = 6081889089893713960L;
    private Long id;
    // -------------------商品基本信息
    //skuId
    private Long skuId;
    //商品编码
    private String thirdSkuId;
    //商品数量
    private Long num;
    //商品原价
    private BigDecimal salePrice;
    //商品总价
    private BigDecimal totalAmount;
    //运费
    private BigDecimal logisticsFee;

    //
    // 条件活动有效数量
    private Long conditionValidNum;
    // 条件活动状态 仅第三组的活动状态-互斥，一个商品最多只可能参加一个互斥活动，所以这里设计的是一个状态值
    private DiscountDetailStatusEnum conditionDiscountDetailStatus;
    // 第四组活动状态-共享，一个商品可能同时参加多个互斥活动，所以这里设计的是map，起名为ShareModel,shareModel中会记录具体参加的那个活动的流水号
    private Map<FrontPromotionTypeEnum, ShareModel> shareModelMap;

    // 优惠券-平台金额券活动状态
    private DiscountDetailStatusEnum couponPlatAmountDiscountDetailStatus;
    // 优惠券-店铺金额券条件活动状态
    private DiscountDetailStatusEnum couponStoreAmountDiscountDetailStatus;
    // 优惠券-代金券状态
    private DiscountDetailStatusEnum couponProxyAmountDiscountDetailStatus;
    // 优惠券-运费券状态
    private DiscountDetailStatusEnum couponFreightDiscountDetailStatus;

    // --------------------单品类活动信息
    // 活动流水号
    private String promotionSerialNo;
    // 活动id
    private String promotionId;
    // 活动编码
    private String promotionCode;
    // 活动类型-单品类
    private FrontPromotionTypeEnum frontPromotionTypeEnum;
    // 是否包邮
    private Boolean parcelsFlag;
    // 状态
    private DiscountDetailStatusEnum discountDetailStatusEnum;
    // 活动创建时间
    private Date promotionCreateTime;
    //活动优惠金额-单品类
    private BigDecimal promotionDiscountTotalAmount;
    //优惠总运费-单品类
    private BigDecimal promotionDiscountFreightAmount;
    // 单品活动有效数量，假如一个商品有5个，但是真正参加活动的可能为3个（可能被限量了，也可能是其它规则），所以这里单独存的是参加活动的有效数量
    private Long singleValidNum;
    /** 是否叠加应用，次数，-1否，n次数，叠加型活动专用,如满额 */
    private Integer overlayAppNum;
    /** 是否叠加应用,0否，1是, */
    private Boolean overlayAppFlag;
    // 校验的结果
    private String singleMessage;
    // 渠道
    private String channelCodes;
    // 会员类型
    private String memTypes;
    // 等级
    private List<PromotionMemberGrade> promotionMemberGradeList;

    // 拼团
    private Boolean launchOrJoinSuccess;
    private String grouponInstCode;
    private Integer launchFlag;
    private List<PostPayGroupInstDetailInfo> groupInstDetailInfoList;

    // 秒杀
    private List<SkillTime> skillTimeList;
    // 超限规则，0按原价购买 1不可以购买
    private String overLimitRule;

    // 预售
    // 预售价
    BigDecimal salePriceWithTax;
    // 订金
    BigDecimal frontMoney;
    // 订金抵扣额
    BigDecimal frontMoneyDeductAmount;

    public void setDiscountStatus(ConditionDiscountModel conditionDiscountModel, DiscountDetailStatusEnum status) {
        if(conditionDiscountModel.getFrontPromotionTypeEnum() == FrontPromotionTypeEnum.COUPON_){
            QueryCouponPersonResponse couponInfo = conditionDiscountModel.getCouponInfo();
            if(null != couponInfo){
                if(couponInfo.getAmountDeductType().equals(0) && couponInfo.getCouponPlatform().equals(1)){//平台商品券
                    setCouponPlatAmountDiscountDetailStatus(status);
                }
                else if(couponInfo.getAmountDeductType().equals(0) && couponInfo.getCouponPlatform().equals(2)) {//店铺商品券
                    setCouponStoreAmountDiscountDetailStatus(status);
                }
                else if(couponInfo.getAmountDeductType().equals(1)) {//运费券
                    setCouponFreightDiscountDetailStatus(status);
                }
                else if(couponInfo.getAmountDeductType().equals(2)) {//待金券
                    setCouponProxyAmountDiscountDetailStatus(status);
                }
            }
        }
        else if(conditionDiscountModel.getFrontPromotionTypeEnum().getGroup().equals(3)){
            setConditionDiscountDetailStatus(status);
        }
        else if(conditionDiscountModel.getFrontPromotionTypeEnum().getGroup().equals(2)){
            if(null == shareModelMap) {
                shareModelMap = new HashMap<>();
            }
            ShareModel shareModel = shareModelMap.get(conditionDiscountModel.getFrontPromotionTypeEnum());
            if(null == shareModel){
                shareModel = new ShareModel();
                shareModel.setPromotionSerialNo(conditionDiscountModel.getPromotionSerialNo());
                shareModel.setFrontPromotionTypeEnum(conditionDiscountModel.getFrontPromotionTypeEnum());
                shareModel.setConditionDiscountDetailStatus(status);
                shareModelMap.put(conditionDiscountModel.getFrontPromotionTypeEnum(), shareModel);
            }else {
                shareModel.setConditionDiscountDetailStatus(status);
            }
        }
    }

    public DiscountDetailStatusEnum getDiscountStatus(ConditionDiscountModel conditionDiscountModel) {
            if(conditionDiscountModel.getFrontPromotionTypeEnum() == FrontPromotionTypeEnum.COUPON_){
                QueryCouponPersonResponse couponInfo = conditionDiscountModel.getCouponInfo();
                if(null != couponInfo){
                    if(couponInfo.getAmountDeductType().equals(0) && couponInfo.getCouponPlatform().equals(1)){//平台商品券
                        return getCouponPlatAmountDiscountDetailStatus();
                    }
                    else if(couponInfo.getAmountDeductType().equals(0) && couponInfo.getCouponPlatform().equals(2)) {//店铺商品券
                        return getCouponStoreAmountDiscountDetailStatus();
                    }
                    else if(couponInfo.getAmountDeductType().equals(1)) {//运费券
                        return getCouponFreightDiscountDetailStatus();
                    }
                    else if(couponInfo.getAmountDeductType().equals(2)) {//待金券
                        return getCouponProxyAmountDiscountDetailStatus();
                    }
                }
            }
            else if(conditionDiscountModel.getFrontPromotionTypeEnum().getGroup().equals(3)){
                getConditionDiscountDetailStatus();
            }
            else if(conditionDiscountModel.getFrontPromotionTypeEnum().getGroup().equals(2)){
                if(null == shareModelMap) {
                    shareModelMap = new HashMap<>();
                }
                ShareModel shareModel = shareModelMap.get(conditionDiscountModel.getFrontPromotionTypeEnum());
                if(null != shareModel){
                    return shareModel.getConditionDiscountDetailStatus();
                }
            }
        return null;
    }


    public void calcuDiscountFreightAmount(Boolean parcelsFlag){
        if(null == this.logisticsFee){
            return ;
        }
        if(null != parcelsFlag && parcelsFlag){
            this.promotionDiscountFreightAmount = this.logisticsFee;
        }
    }
    /**
     * 计算并设置优惠的邮费，返回还需要分配剩余的总优惠邮费
     * @param discountTotalLogisticsFee
     * @return
     */
    public void calcuDiscountFreightAmount(BigDecimal discountTotalLogisticsFee){
        if(null == this.logisticsFee){
            return ;
        }
        if(discountTotalLogisticsFee.compareTo(this.logisticsFee) == 1){
            this.promotionDiscountFreightAmount = this.logisticsFee;
            discountTotalLogisticsFee = Money.subtract(discountTotalLogisticsFee, this.logisticsFee);
        }else {
            this.promotionDiscountFreightAmount = discountTotalLogisticsFee;
            discountTotalLogisticsFee = BigDecimal.ZERO;
        }
    }

    @Override
    public Integer getSort() {
        return 0;
    }

}
